package db

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
)

var DB *gorm.DB
var mysqlLogger logger.Interface

func init() {
	user := "talon"
	password := "Gao123456@1"
	host := "192.168.71.6"
	port := 3306
	dbname := "gorm_learning"
	timeout := "10s"

	mysqlLogger = logger.Default.LogMode(logger.Info)

	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local&timeout=%s", user, password, host, port, dbname, timeout)
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("数据库连接失败， error：" + err.Error())
	}
	DB = db.Session(&gorm.Session{
		Logger: mysqlLogger,
	})
}
